package service.impl;

import dao.AccountDao;
import dao.UserDao;
import dao.impl.AccountDaoImpl;
import dao.impl.UserDaoImpl;
import entity.Account;
import entity.User;
import service.UserService;
import utils.InfoUtils;
import java.util.List;

/**
 * @program: FinalExam
 * @description:
 * @create: 2021-12-14 17:12
 * @Date 2021/12/14
 **/

public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserDaoImpl();
    private AccountDao accountDao = new AccountDaoImpl();

    public User adminLogin(String username, String password) {
        return userDao.adminLogin(username,password);
    }

    public User userLogin(String username, String password) {
        return userDao.userLogin(username,password);
    }

    /**
     * 登录功能
     * @param username
     * @param password
     * @return
     */
    public User login(String username, String password) {
        User login = userDao.login(username, password);
        return login;
    }

    public String addUser(User user) {
        // TODO 随机生成 用户 id 和 6位的账号
        String id = InfoUtils.RandomSixNumber();
        String username = InfoUtils.RandomSixNumber();
        user.setId(id);
        user.setUsername(username);
        String gmt_create = InfoUtils.getTime();
        user.setGmt_create(gmt_create);
        user.setStatus(0);
        Boolean isSuccess = false;
        User exist = userDao.findByIdAndUsername(id,username);
        if(exist==null){
            // 对于User表的操作
            Boolean addUser = userDao.addUser(user);
            // 对于Account表的操作
            String accountId= InfoUtils.RandomSixNumber();
            Account account = accountDao.findByAccountId(accountId);
            if(account==null){
                // 代表没有此账号需要进行开户处理
                 accountDao.addAccount(accountId, gmt_create, id);
                 System.out.println("");
                 return username;
            }
        }

        return null;
    }

    public Boolean deleteUserById(String id) {
        String gmt_modify = InfoUtils.getTime();
        return userDao.deleteUserById(id,gmt_modify);
    }

    public Boolean modifyUserInfoById(String id, User user) {
        String gmt_modify = InfoUtils.getTime();
        user.setGmt_modify(gmt_modify);
        return  userDao.modifyUserInfoById(id,user);
    }

    public List<User> ShowUsersInfo() {
        return userDao.ShowUsersInfo();
    }

    public User findById(String id) {
        return userDao.findById(id);
    }

    public void modifyPassword(String id, String password) {
        userDao.modifyPassword(id,password);
    }

    public List<User> findAllUserInfo() {
        return userDao.findAllUserInfo();
    }

    public User findByUsername(String username){
        return userDao.findByUsername(username);
    }

}
